package com.gaohe.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]...");
        if (metaObject.hasSetter("createTime")){
            metaObject.setValue("createTime", LocalDateTime.now());
        }
        if (metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime",LocalDateTime.now());
        }
        //获取管理员 id
        Long userid = (Long) BaseContext.getCurrentId();
        if (metaObject.hasSetter("createUser")){
            metaObject.setValue("createUser",userid);
        }
        if (metaObject.hasSetter("updateUser")){
            metaObject.setValue("updateUser",userid);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]...");
        if (metaObject.hasSetter("updateTime")){
            metaObject.setValue("updateTime",LocalDateTime.now());
        }
        //Long userid = 1l;
        Long userid = (Long) BaseContext.getCurrentId();
        if (metaObject.hasSetter("updateUser")){
            metaObject.setValue("updateUser",userid);
        }
    }
}
